#include <stdio.h>
#include <stdlib.h>
#include "../lib/scicalc.h"
#include <math.h>
#include <string.h>

#define EPSILON 0.0001
#define MATRIX_SIZE 3

// The correct matrix that we are using in the following tests is a 2x3 matrix
// {1.0, 0.0, 0.0)
// {0.0, 5.0, 0.0}
// All the following sparse or dense matrix should be the same as this one, otherwise they are wrong

void test_sparse_to_dense() {
    double sparse[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
    int rows = 2, cols = 3;
    double* dense = sparse_to_dense(sparse, rows, cols);
    
    double expected[] = {1.0, 0.0, 0.0, 0.0, 5.0, 0.0};
    for(int i = 0; i < rows*cols; i++) {
        if(fabs(dense[i] - expected[i]) > EPSILON) {
            printf("Test sparse_to_dense failed at index %d: expected %.2f, got %.2f\n", 
                   i, expected[i], dense[i]);
            exit(1);
        }
    }
    free(dense);
    free(dense);
}

void test_dense_to_sparse() {
    double dense[] = {1.0, 0.0, 0.0, 0.0, 5.0, 0.0};
    int rows = 2, cols = 3;
    double* sparse = dense_to_sparse(dense, rows, cols);
    
    double expected[] = {1.0, 6.0};
    int expected_len = 2;
    for(int i = 0; i < expected_len; i++) {
        if(fabs(sparse[i] - expected[i]) > EPSILON) {
            printf("Test dense_to_sparse failed at index %d: expected %.2f, got %.2f\n", 
                   i, expected[i], sparse[i]);
            exit(1);
        }
    }
    free(sparse);
    printf("%f", sparse[0]);
}

void test_empty_matrix() {
    double empty[] = {};
    double* result = sparse_to_dense(empty, 0, 0);
    if(result != NULL) {
        printf("Test empty_matrix failed: expected NULL, got non-NULL\n");
        free(result);
        exit(1);
    }
}

int main() {
    test_sparse_to_dense();
    test_dense_to_sparse();
    test_empty_matrix();
    
    printf("All tests passed!\n");
    return 0;
}